//
//  HMVisitorViewController.swift
//  WeiBo14
//
//  Created by HM on 16/3/3.
//  Copyright © 2016年 HM. All rights reserved.
//

import UIKit

class HMVisitorViewController: UITableViewController {
    
    // 用户是否登录
    var userLogon: Bool = HMUserAccountViewModel.sharedAccountViewModel.userLogon
    
    // 设置当前控制器的view的
    override func loadView() {
        // super.loadView()
        if userLogon {
            // 用户登录,直接显示tableView就可以了
            super.loadView()
        }else {
            // 如果没有登录,那么就登录我们的访客视图
            setupVisitorView()
        }
    }
    
    /// 设置访客视图
    private func setupVisitorView(){
        view = visitorView
        
        // 设置右左的登录按钮
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "注册", target: self, action: "login")
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "登录", target: self, action: "login")
        // 这种直接拿到该按钮添加点击事件的方法也是可以的
//        visitorView.registerButton.addTarget(self, action: "login", forControlEvents: UIControlEvents.TouchUpInside)
    }
    
    @objc private func login(){
        
        // 弹出登录的控制器
        let vc = HMOAuthViewController()
        presentViewController(HMNavigationController(rootViewController: vc), animated: true, completion: nil)
    }
    
    
    // MARK: - 懒加载控件
    
    
    lazy var visitorView: HMVisitorView = {
        let v = HMVisitorView()
        v.delegate = self
        return v
    }()
}

extension HMVisitorViewController: HMVisitorViewDelegate {
    func visitorView(visitorView: HMVisitorView, loginButtonDidCliked: UIButton) {
        // 执行登录操作
        login()
    }
}